package com.smartrobot.smartrobot.pagerHolder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import com.smartrobot.smartrobot.R;

import com.smartrobot.smartrobot.activity.SocialAddActivity;

import com.smartrobot.smartrobot.adapter.SocialListViewAdapter;

import com.smartrobot.smartrobot.circle.CircleUtil;

import com.smartrobot.smartrobot.util.ImageLoaderApi;

import com.smartrobot.smartrobot.view.ExTopListView;


public class SocialCirlePagerHolder2 extends Fragment implements BasePagerHolder {

    private View view;
    private boolean isFirst = true;
    private boolean isCreate;
    static FragmentActivity instance;

    //组件ExListView
    private ExTopListView listView;
    private TextView progressTxt;
    private ImageView add;


    private final static int RETURN_FROM_ADD_ACTIVITY = 10;

    public SocialCirlePagerHolder2(){
        super();
        isCreate = false;

    }

    /**
     * 返回的view为滑动显示的view
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        isFirst = true;
        isCreate = true;
        view = inflater.inflate(R.layout.page_social_circle_my_circle2, container, false);
        init();
        Log.d("-========socialCirclePagerHolder2对象 oncreate======",SocialCirlePagerHolder2.this.toString());


        return view;
    }



    @Override
    public void onStart() {
        super.onStart();

    }



    public void init(){

        Log.d("-========socialCirclePagerHolder2对象======",SocialCirlePagerHolder2.this.toString());




        if(isCreate)
        if(isFirst||CircleUtil.isFriendChange){

//            initView();
            isFirst = false;
            CircleUtil.isFriendChange = false;

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                    ImageLoader.getInstance().init(ImageLoaderApi.getImageLoaderConfig(getActivity()));
                    listView = (ExTopListView)view.findViewById(R.id.circle_listview);
                    progressTxt = (TextView)view.findViewById(R.id.progress_txt);
                    add = (ImageView)view.findViewById(R.id.social_circle_add_friend);

                    SocialListViewAdapter adapter = new SocialListViewAdapter(getActivity(),listView);



                    listView.setAdapter(adapter);
                    listView.setOnUdateNewListender(new ExTopListView.UdateNewListener() {
                        @Override
                        public void updateNew() {

                            Log.e("===================>","listview 下拉需要更新 " );
                            if(SocialListViewAdapter.instance!=null){
                                SocialListViewAdapter.instance.getCircleMsgFromServer();
                            }else{
                                CircleUtil.isFriendChange = true;
                            }

                        }
                    });


                    add.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent();
                            intent.setClass(getActivity(),SocialAddActivity.class);
                            startActivityForResult(intent, RETURN_FROM_ADD_ACTIVITY);
                        }
                    });


                    progressTxt.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);

                }
            },0);

        }
    }

//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        this.activity = activity;
//    }

    @Override
    public void setView(View view) {

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("get activity for result !!!!!","");
        super.onActivityResult(requestCode, resultCode, data);
    }

    //for result
    public static  void changeFriendList(int requestCode, int resultCode, Intent data){
        Log.e("for result !!!!!!!!!!","返回");
        SocialListViewAdapter.changeFriendList(requestCode,resultCode,data);
    }

}
